package com.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.Organization;
import com.example.service.OrganizationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "组织管理接口")
@RestController
@RequestMapping("/api/organizations")
@RequiredArgsConstructor
public class OrganizationController {

    private final OrganizationService organizationService;

    @Operation(summary = "注册组织")
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody Organization organization) {
        return Result.success(organizationService.registerOrganization(organization));
    }

    @Operation(summary = "更新组织信息")
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody Organization organization) {
        return Result.success(organizationService.updateOrganization(organization));
    }

    @Operation(summary = "审核组织")
    @PostMapping("/{id}/review")
    public Result<Boolean> review(
            @Parameter(description = "组织ID") @PathVariable Long id,
            @Parameter(description = "状态：1-通过 2-驳回") @RequestParam Integer status,
            @Parameter(description = "驳回原因") @RequestParam(required = false) String rejectReason,
            @Parameter(description = "审核人ID") @RequestParam Long reviewerId) {
        return Result.success(organizationService.reviewOrganization(id, status, rejectReason, reviewerId));
    }

    @Operation(summary = "启用/禁用组织")
    @PostMapping("/{id}/toggle")
    public Result<Boolean> toggleStatus(
            @Parameter(description = "组织ID") @PathVariable Long id,
            @Parameter(description = "是否启用") @RequestParam boolean enable) {
        return Result.success(organizationService.toggleOrganizationStatus(id, enable));
    }

    @Operation(summary = "删除组织")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@Parameter(description = "组织ID") @PathVariable Long id) {
        return Result.success(organizationService.deleteOrganization(id));
    }

    @Operation(summary = "批量删除组织")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDelete(@Parameter(description = "组织ID数组") @RequestBody Long[] ids) {
        return Result.success(organizationService.batchDeleteOrganizations(ids));
    }

    @Operation(summary = "分页查询组织列表")
    @GetMapping("/list")
    public Result<Page<Organization>> list(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "关键字") @RequestParam(required = false) String keyword,
            @Parameter(description = "类型") @RequestParam(required = false) Integer type,
            @Parameter(description = "级别") @RequestParam(required = false) Integer level,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        return Result.success(organizationService.getOrganizationList(pageNum, pageSize, keyword, type, level, status));
    }

    @Operation(summary = "获取组织详情")
    @GetMapping("/{id}")
    public Result<Organization> getInfo(@Parameter(description = "组织ID") @PathVariable Long id) {
        return Result.success(organizationService.getOrganizationInfo(id));
    }
} 